package com.example.demo.springbootdemo.demos;


import com.example.demo.springbootdemo.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/demo1")
public class ParamsController {
    // 就传入一个参数 + 改变请求方法
    @RequestMapping(value = "/m1", method = RequestMethod.POST)
    public String m1(String name) {
        return "this name is " + name;
    }

    // 注意： 属性名称必须与传入对应的属性名相同
    @RequestMapping("/m2")
    public String m2(String name, Integer age) {
        return "this name is " + name + ", age is " + age;
    }

    // 经行重命名 （前端就在前面 后端的就在后面） 默认是必传 可以设为不必传
    @RequestMapping("/m3")
    public String m3(@RequestParam(value = "name", required = false) String username, Integer age) {
        return "this username is " + username + ", age is " + age;
    }
    // 考虑到扩展性 建议传入一个对象
    @RequestMapping("/m4")
    public String m4(Person person) {
        return "person is " + person.toString();
    }

    // 传数组 需要名称一样 其余的和普通传参没什么区别
    @RequestMapping("/m5")
    public String m5(String[] arrayParam) {
        return "arrayParam: " + Arrays.toString(arrayParam) + ", len: " + arrayParam.length;
    }

    // 传集合List 为了区别数组 需要加上 @RequestParam
    @RequestMapping("/m6")
    public String m6(@RequestParam List<String> listParam) {
        return "arrayParam: " + listParam + ", len: " + listParam.size();
    }

    // 传入 JSON
    @RequestMapping("/m7")
    public String m7(@RequestBody Person person) {
        return "Person: " + person.toString();
    }

    // 获取 URL 中的参数 如果路径中写了 那么就必传 不可以修改,写的参数 是默认必传 可以修改
    @RequestMapping("/m8/{userId}/{name}")
    public String m8(@PathVariable Integer userId, @PathVariable(required = false) String name) {
        System.out.println("userId: " + userId + ", name: " + name);
        return "userId: " + userId + ", name: " + name;
    }

    // 图片（文件）
    @RequestMapping("/m9")
    public String m9(@RequestPart MultipartFile file) throws IOException {
        System.out.printf(file.getOriginalFilename());
        return "文件上传成功";
    }

    // 获取cookies
    // 1. 通过内置对象来获取 HttpServletRequest
    @RequestMapping("/getCookie1")
    public String getCookie1(HttpServletRequest request)  {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            Arrays.stream(cookies).forEach((v) -> {
                System.out.println(v.getName() + ":" + v.getValue());
            });
        }
        return "获取cookie";
    }
    // 2. 通过注解来获取 不过一次只能获取一个 和之前的属性一样 名字必须要相同
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue String username)  {
        return "获取cookie的username: " + username;
    }

    // 获取 session
    @RequestMapping("/setSession")
    public String setSession(HttpSession session) {
        session.setAttribute("username", "zhaoliu");
        session.setAttribute("addr", "diqiu");
        return "session 设置成功";
    }
    // 1. 通过内置对象来获取
    @RequestMapping("/getSession1")
    public String getSession1(HttpServletRequest request)  {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        return "获取session1的username: " + username;
    }
    // 2. 通过注解来获取 session属性 同理 一次只能获取一个
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute String username, @SessionAttribute String addr)  {
        return "获取session2的username: " + username + ", addr: " + addr;
    }
    // 3. 直接通过内置对象 HttpSession 来获取
    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session)  {
        return "获取session3的username: " + session.getAttribute("username");
    }

    // 获取请求头
    // 1. 通过request 来获取
    @RequestMapping("/getHeader1")
    public String getHeader1(HttpServletRequest request)  {
        String userAgent = request.getHeader("User-Agent");
        return "获取请求头1的userAgent: " + userAgent;
    }
    // 2. 通过注解来获取 来获取
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent)  {
        return "获取请求头2的userAgent: " + userAgent;
    }
}
